home *** CD-ROM | disk | FTP | other *** search
/ HTML Publishing on the Internet / html_publishing_on_the_internet_01.iso / Filemaker Pro_CGI / ROFM_CGI / Encode URL ƒ / Encode URL.c next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.3 KB  |  131 lines  |  [TEXT/KAHL]

  1. /********************************************
  2.  * Encode URL OSAX - by Chuck Shotton, 10/17/94
  3.  * This code, the OSAX, and all of its constituent parts are hereby
  4.  * placed in the public domain.
  5.  *
  6.  * This code demonstrates how to build an OSAX for use with MacHTTP
  7.  * and AppleScript. This particular example encodes special chars
  8.  * in form args and URLs passed as strings to it using %xx encodings.
  9.  *
  10.  * This code was written using Think C, but should build with no
  11.  * problems under CodeWarrior as well. It should be build as a 68k
  12.  * code resource to avoid problems. The code resource type is 'osax'
  13.  * and its resource ID MUST match the event class and code.
  14.  * For this event the name is "AEVTaevteURL", indicating that this is
  15.  * an AppleEvent, class aevt, code eURL.
  16.  *********************************************/
  17.  
  18. #include <AppleEvents.h> 
  19.  
  20. /*********************************************/
  21.  
  22. void CharToHex (unsigned char c, char *s)
  23. {
  24. unsigned char x;
  25.     s[0] = '%';
  26.     x = (c & 0xF0) >> 4;
  27.     s[1] = x>9 ? (x-10)+'A' : x+'0';
  28.     x = c & 0x0F;
  29.     s[2] = x>9 ? (x-10)+'A' : x+'0';
  30. }
  31.  
  32. /*********************************************/
  33.  
  34. void EncodeChar (unsigned char c, char *s, int *j)
  35. {
  36. char *bad = " $&=+\"\\';/#?:";
  37. int i;
  38.     if (c<33 || c>126) {
  39.         CharToHex (c, &s[*j]);
  40.         *j+=3;
  41.     }
  42.     else {
  43.         for (i=0;i<14;i++)
  44.             if (c == bad[i]) {
  45.                 CharToHex (c, &s[*j]);
  46.                 *j+=3;
  47.                 break;
  48.             }
  49.         if (i==14)
  50.             s[(*j)++] = c;
  51.     }
  52.     
  53.  
  54. }
  55.  
  56. /*********************************************/
  57.  
  58. pascal OSErr main(    AppleEvent *theAEEvent, 
  59.                             AppleEvent *theReply, 
  60.                             long *theRefCon) 
  61.  
  62. {     
  63.  
  64.     /* variables */
  65.     OSErr            theErr = noErr;     
  66.     DescType         typeCode;
  67.     Size             sizeOfParam,
  68.                     actualSize;
  69.     char *url, *encoded;
  70.     int i, j;
  71.     unsigned char bite;
  72.     
  73.     theErr = AESizeOfParam(    theAEEvent,
  74.                                keyDirectObject,
  75.                                &typeCode,
  76.                                &sizeOfParam);
  77.   
  78.     if (theErr != noErr){            
  79.         return theErr;
  80.     }     
  81.     else{  
  82.         /*try converting lots of types of strings into something we can use*/
  83.         if ((typeCode == typeChar) || (typeCode == typeStyledText) || 
  84.             (typeCode == typeIntlText)) {        
  85.         
  86.             /*Make a place to store the parameter, then get it.*/
  87.             if (url = (char *) NewPtr (sizeOfParam+2)) {
  88.                 theErr = AEGetParamPtr(theAEEvent, keyDirectObject, typeChar, 
  89.                                          &typeCode, (Ptr) url, 
  90.                                         sizeOfParam+1, &actualSize);
  91.                                         
  92.                 if(theErr == noErr) {
  93.                     /* 'C' string has a null as last char */
  94.                     url [actualSize] = '\0';
  95.                     i=0;
  96.                     j = 0;
  97.                     
  98.                     /*make a buffer to hold the encoded string*/
  99.                     if (actualSize*3 > 32760)
  100.                         sizeOfParam = 32766;
  101.                     else
  102.                         sizeOfParam = actualSize*3+3;
  103.                     
  104.                     if (encoded = (char *) NewPtr (sizeOfParam)) {
  105.                         /*create the %xx encodings*/
  106.                         while ( url[i] && j<sizeOfParam-2) {
  107.                             EncodeChar (url[i++], encoded, &j);
  108.                         }
  109.                         encoded [j] = '\0';
  110.                     }
  111.                     else {
  112.                         DisposPtr (url);
  113.                         return errAEEventNotHandled;
  114.                     }
  115.                 }
  116.                 /*return the result of any processing to the caller*/
  117.                 if (theReply->descriptorType != typeNull) 
  118.                     theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar,
  119.                         encoded, j );
  120.                 DisposPtr (url);
  121.                 DisposPtr (encoded);
  122.             }
  123.             else return errAEEventNotHandled;
  124.         }
  125.         else // Wasn't a string so bail
  126.             return errAEEventNotHandled;
  127.     } 
  128.         
  129.     return theErr;
  130.  }
  131.